package com.x.service.user;

import com.x.bean.Achievement;
import com.x.bean.User;
import com.x.dao.BaseDao;
import com.x.dao.user.UserDao;
import com.x.dao.user.UserDaoImpl;
import org.junit.*;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

/**
 * @author 13925
 */
public class UserServiceImpl implements UserService {
    /**
     * 业务层都会调用dao层，所以我们要引入Dao层；
     */
    private UserDao userDao;

    public UserServiceImpl() {
        userDao = new UserDaoImpl();
    }

    @Override
    public User login(String code, String password, int grade) {
        Connection connection = null;
        //通过业务层调用对应的具体数据库操作
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getLoginUser(connection, code, password, grade);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public boolean addUser(User user) {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            //开启JDBC事务管理
            connection.setAutoCommit(false);
            int updateRows = userDao.addUser(connection, user);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
            } else {

            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean deleteUserByCode(String code) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int updateRows = userDao.deleteUserByName(connection, code);
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean updateUser(User user) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int updateRows = userDao.updateUser(connection, user);
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public User getUserByCode(String code) {
        Connection connection = null;
        //通过业务层调用对应的具体数据库操作
        User user = null;
        try {
            connection = BaseDao.getConnection();
            user = userDao.getUserByCode(connection, code);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return user;
    }

    @Override
    public List<User> getUserList() {
        Connection connection = null;
        List<User> userList = null;
        try {
            connection = BaseDao.getConnection();
            userList = userDao.getUserList(connection);
        } catch (Exception e) {
            e.printStackTrace();
        }finally{
            BaseDao.closeResource(connection, null, null);
        }
        return userList;
    }

    @Override
    public boolean addAchievement(Achievement achievement) throws SQLException {
        boolean flag = false;
        Connection connection = null;
        try {
            connection = BaseDao.getConnection();
            //开启JDBC事务管理
            connection.setAutoCommit(false);
            int updateRows = userDao.addAchievement(connection, achievement);
            connection.commit();
            if (updateRows > 0) {
                flag = true;
            } else {
            }
        } catch (Exception e) {
            e.printStackTrace();
            try {
                connection.rollback();
            } catch (SQLException e1) {
                e1.printStackTrace();
            }
        } finally {
            //在service层进行connection连接的关闭
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean deleteAchievementByCodeAndCourseName(String code, String courseName) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int updateRows = userDao.deleteAchievementByCodeAndCourseName(connection, code, courseName);
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public boolean updateAchievement(Achievement achievement) {
        Connection connection = null;
        boolean flag = false;
        try {
            connection = BaseDao.getConnection();
            int updateRows = userDao.updateAchievement(connection, achievement);
            if (updateRows > 0) {
                flag = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return flag;
    }

    @Override
    public List<Achievement> getAchievementByCode(String code) {
        Connection connection = null;
        //通过业务层调用对应的具体数据库操作
        List<Achievement> achievementsList = null;
        try {
            connection = BaseDao.getConnection();
            achievementsList = userDao.getAchievementByCode(connection, code);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            BaseDao.closeResource(connection, null, null);
        }
        return achievementsList;
    }
}
